package com.example.lib.controller;

import com.example.lib.model.Book;
import com.example.lib.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("book")
public class BookController extends  BaseController {

    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping(value = "addBook")
    private String addBook(Book book) {
        bookService.create(book);
        List<Book> books = bookService.queryAll();
        session.setAttribute("books", books);
        return "redirect:/user/index.jsp";
    }

    @GetMapping(value = "removeById/{id}")
    private String removeById(@PathVariable("id") int id) {
        bookService.remove(id);
        session.setAttribute("books", bookService.queryAll());
        return "redirect:/user/index.jsp";
    }

    @GetMapping(value = "detailById/{id}")
    private String detailById(@PathVariable("id") int id) {
        Book book = bookService.queryById(id);
        request.setAttribute("book", book);
        return "/user/detail_book.jsp";
    }
}